package JDBC_TEST;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yang
 * Date: 2022-08-10
 * Time: 16:46
 */
public class JDBC {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/bite?characterEncoding=UTF-8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1100ppqqaall");

        Connection connection = dataSource.getConnection();
        Scanner scan = new Scanner(System.in);
        System.out.println("输入您的姓名:");
        String name = scan.nextLine();
        System.out.println("输入您的年龄:");
        int age = scan.nextInt();
        String sql = "insert into student values(? , ?);";//问号为占位符
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setInt(2,age);
        statement.executeUpdate();

        statement.close();
        connection.close();
    }

}
